var $jquery = require('jquery');
var $counter = require('./counter');
var $http = require('http');



var server = $http.createServer(function(req,res){
	//
	$counter.getAllData(
		function(items){	
			res.write( JSON.stringify({alldata:items}) );
					
			res.end();
		},
		function(err){
			res.write(err);
			res.end();
		}
	);

}).listen(3000);



var $site = "http://www.apple.com/itunes/store/counters/il6ark7ec.js";
function refreshData()
{
	$jquery.getJSON($site, 
	function(json)
	{
		//check if in db already 
		$counter.addData(json, 
			function(){  //sucess
				console.log(json);
				setTimeout(refreshData,900000);
			}, 
			function(err){  //fail
				console.log(err);
				setTimeout(refreshData,900000);
			}
		);
	} 
	);
}

refreshData();